package org.misty.practices.threadpool;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Misty on 2020-06-12
 */
public class ThreadPoolExecutorSample01 {
    public static void main(String[] args) throws InterruptedException {
        final CountDownLatch countDownLatch = new CountDownLatch(100);
        final AtomicInteger counter = new AtomicInteger(0);
        BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, Integer.MAX_VALUE, TimeUnit.MILLISECONDS, queue);
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            executor.execute(() -> {
                int id = counter.incrementAndGet();
                System.out.println("start " + id);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("end " + id);
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        long endTime = System.currentTimeMillis();
        System.out.println(endTime - startTime);

        countDownLatch.countDown();
    }
}
